package com.gdust.gkyx.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdust.gkyx.common.result.Result;
import com.gdust.gkyx.model.product.Category;
import com.gdust.gkyx.product.service.CategoryService;
import com.gdust.gkyx.vo.product.CategoryQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: CategoryController
 * Package: com.gdust.gkyx.product.controller
 * Description:商品三级分类 前端控制器
 *
 * 参考前端src >api>product > category.js进行接口编写
 *
 * @Author syun
 * @Create 2025/1/25 9:21
 * @Version 1.0
 */
@Api(tags = "商品三级分类接口")
@RestController
@RequestMapping("/admin/product/category")
//@CrossOrigin
public class CategoryController {
    //注入商品三级分类表色service
    @Autowired
    private CategoryService categoryService;

    /**
     *  //商品分类列表
     * //    url: `${api_name}/${page}/${limit}`,
     * //    method: 'get',
     * //    params: searchObj
     *
     * @param page 当前页
     * @param limit 每页记录数
     * @param categoryQueryVo 分页条件
     * @return
     */
    @ApiOperation("商品分类列表")
    @GetMapping("{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit,
                       CategoryQueryVo categoryQueryVo) {
        Page<Category> pageParam = new Page<>(page,limit);
        //自定义方法：商品分类列表
        IPage<Category> pageModel = categoryService.selectPageCategory(pageParam,categoryQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 获取商品分类信息
     * @param id 分类id
     * @return
     */
    @ApiOperation(value = "获取商品分类信息")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        return Result.ok(category);
    }

    /**
     * 新增商品分类
     * @param category 种类对象
     * @return
     */
    @ApiOperation(value = "新增商品分类")
    @PostMapping("save")
    public Result save(@RequestBody Category category) {
        boolean is_success = categoryService.save(category);
        if(is_success) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }

    /**
     * 修改商品分类
     * @param category 种类对象
     * @return
     */
    @ApiOperation(value = "修改商品分类")
    @PutMapping("update")
    public Result updateById(@RequestBody Category category) {
        boolean is_success = categoryService.updateById(category);
        if(is_success) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }

    /**
     * 删除商品分类
     * @param id 分类id
     * @return
     */
    @ApiOperation(value = "删除商品分类")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean is_success = categoryService.removeById(id);
        if(is_success) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }

    /**
     * 根据id列表删除商品分类
     * @param idList 分类id集合
     * @return
     */
    @ApiOperation(value = "根据id列表删除商品分类")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean is_success = categoryService.removeByIds(idList);
        if(is_success) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }

    /**
     *  //      url: `${api_name}/findAllList`,
     *     //      method: 'get'
     *     //查询所有商品分类
     * @return
     */
    @ApiOperation("查询所有商品分类")
    @GetMapping("findAllList")
    public Result findAllList() {
        List<Category> list = categoryService.list();
        return Result.ok(list);
    }

}
